package org.liaohailong.victorlibrary.ui

import android.content.Context
import android.content.Intent
import android.graphics.SurfaceTexture
import android.hardware.Camera
import android.os.Bundle
import android.view.Surface
import android.view.TextureView
import kotlinx.android.synthetic.main.activity_camera.*
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity
import kotlin.properties.Delegates

/**
 * Describe: 相机调试
 *
 *
 * @author liaohailong
 * Time: 2020/10/9 13:58
 */
class CameraActivity : BaseActivity(), TextureView.SurfaceTextureListener {
    companion object {
        fun show(context: Context) {
            val intent = Intent(context, CameraActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var frontCameraId by Delegates.notNull<Int>()
    private var backCameraId by Delegates.notNull<Int>()
    private var currentCameraId by Delegates.notNull<Int>()


    private var frontCameraOrientation by Delegates.notNull<Int>()
    private var backCameraOrientation by Delegates.notNull<Int>()

    private var surface: SurfaceTexture? = null
    private var camera: Camera? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera)
        texture_view.surfaceTextureListener = this

        // 初始化相机
        val numberOfCameras = Camera.getNumberOfCameras()
        val cameraInfo = Camera.CameraInfo()
        for (i in 0 until numberOfCameras) {
            Camera.getCameraInfo(i, cameraInfo)
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                frontCameraId = i
                frontCameraOrientation = cameraInfo.orientation
            } else if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                backCameraId = i
                backCameraOrientation = cameraInfo.orientation
            }
        }

        // 默认开启前置
        currentCameraId = frontCameraId

        // 打开相机
        camera = Camera.open(currentCameraId)

        // 调正旋转角度
        camera?.apply { setCameraDisplayOrientation(currentCameraId, this) }

        // 旋转相机
        btn_switch.setOnClickListener { switchCamera() }
    }

    private fun setCameraDisplayOrientation(cameraId: Int, camera: Camera) {
        val info = Camera.CameraInfo()
        Camera.getCameraInfo(cameraId, info)
        val degrees = when (windowManager.defaultDisplay.rotation) {
            Surface.ROTATION_0 -> 0
            Surface.ROTATION_90 -> 90
            Surface.ROTATION_180 -> 180
            Surface.ROTATION_270 -> 270
            else -> 0
        }

        var result: Int

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360
            result = (360 - result) % 360  // compensate the mirror
        } else { // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result)
    }

    override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
        this@CameraActivity.surface = surface
        camera?.setPreviewTexture(surface)
        camera?.startPreview()
    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {

    }

    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
        return true
    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {

    }


    private fun switchCamera() {
        // 释放资源
        camera?.apply {
            stopPreview()
            setPreviewTexture(null)
            release()
        }

        // 切换相机
        val cameraId = if (currentCameraId == frontCameraId) backCameraId else frontCameraId
        currentCameraId = cameraId

        // 打开相机
        camera = Camera.open(currentCameraId)
        surface?.apply {
            camera?.setPreviewTexture(this)
            // 调正旋转角度
            camera?.apply { setCameraDisplayOrientation(currentCameraId, this) }
            camera?.startPreview()
        }
    }
}